﻿/**
 *    Copyright 2012 Yohan Courbe
 *
 *  Licensed under the Apache License, Version 2.0 (the "License 
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *      
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using PacketDotNet;

namespace NetworkAnalyzer.Engines
{
    public class EnginesManager
    {
        private readonly List<Engine> _engines = new List<Engine>();

        public void RegisterEngine(Engine engine, string name)
        {
            engine.EnginesManager = this;
            engine.Name = name;

            _engines.Add(engine);
        }

        public Engine GetEngineByName(string name)
        {
            foreach (var engine in _engines.Where(engine => engine.Name == name))
            {
                return engine;
            }

            throw new UnregistredEngineException();
        }

        public void DispatchPacket(Packet packet)
        {
            var eth = packet as EthernetPacket;

            if (eth == null) return;

            foreach (var engine in _engines)
            {
                engine.AddPacket(packet);
            }

            switch (eth.Type)
            {
                case EthernetPacketType.IpV6:
                case EthernetPacketType.IpV4:
                    {
                        var ip = IpPacket.GetEncapsulated(packet);

                        switch (ip.Protocol)
                        {
                            case IPProtocolType.TCP:
                                {
                                    var tcp = TcpPacket.GetEncapsulated(packet);

                                    foreach (var engine in _engines)
                                    {
                                        engine.AddIpTcpPacket(packet, ip, tcp);
                                    }
                                }
                                break;

                            case IPProtocolType.UDP:
                                {
                                    var udp = UdpPacket.GetEncapsulated(packet);

                                    foreach (var engine in _engines)
                                    {
                                        engine.AddIpUdpPacket(packet, ip, udp);
                                    }
                                }
                                break;
                        }
                    }
                    break;
            }
        }

        public void StartAll()
        {
            foreach (var engine in _engines)
            {
                engine.Start();
            }
        }

        public void StopAll()
        {
            foreach (var engine in _engines)
            {
                engine.Stop();
            }
        }
    }

    public class UnregistredEngineException : Exception
    {
    }
}
